class TestTypesController < ApplicationController

  # train_input is called via ajax - no layout wanted.
  layout 'application', :except => 'train_input'

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @title1 = 'Test types'
    @url1 = '/test_types/'
    @test_type_pages, @test_types = paginate :test_types, :per_page => 10
  end

  def show
    @test_type = TestType.find(params[:id])
    @build_type = BuildType.find @test_type.build_type
    @title1 = 'Test types'
    @url1 = '/test_types/'
    @title2 = @test_type.name
    @url2 = "/test_types/show/#{params[:id]}"
    
    dir = "./repository/tests/#{@test_type.name}-#{@test_type.build_type.title}"
    fd = File.open "#{dir}/pass/train.in"
    @test_input = fd.read
    fd.close
    fd = File.open "#{dir}/pass/train.out"
    @test_output = fd.read
    fd.close
    fd = File.open "#{dir}/pass/train.out2"
    @test_output2 = fd.read
    fd.close
    fd = File.open "#{dir}/pass/train.err"
    @test_error = fd.read
    fd.close
  end

  def new
    @title1 = 'Test types'
    @url1 = '/test_types/'
    @title2 = 'New'
    @url2 = "/test_type/new"
    @build_types = BuildType.find_all
    @test_type = TestType.new
  end

  def create
    @title1 = 'Test types'
    @url1 = '/test/types'
    @title2 = 'New'
    @url2 = '/test_type/new'
    @build_types = BuildType.find_all
    if params[:test_type][:timeout].length == 0 then
      flash[:error] = 'A timeout must be specified.'
      render :action => 'new'
      return
    end
    @test_type = TestType.new(params[:test_type])
    if @test_type.save
    
      # Create directory structure.
      dir = "./repository/tests/#{@test_type.name}-#{@test_type.build_type.title}"
      `mkdir #{dir}`
      `mkdir #{dir}/pass`
      `touch #{dir}/pass/train.in`
      `touch #{dir}/pass/train.out`
      `touch #{dir}/pass/train.out2`
      `touch #{dir}/pass/train.err`
    
      render :action => 'confirm'
    else
      render :action => 'new'
    end
  end

  def edit
    @title1 = 'Test types'
    @url1 = '/test_types/'
    @build_types = BuildType.find_all
    @test_type = TestType.find(params[:id])
    @title2 = "Edit #{@test_type.name}"
    @url2 = "/test_types/edit/#{params[:id]}"
  end

  def update
    @test_type = TestType.find(params[:id])
    if @test_type.update_attributes(params[:test_type])
      flash[:notice] = 'TestType was successfully updated.'
      redirect_to :action => 'show', :id => @test_type
    else
      render :action => 'edit'
    end
  end

  def train
    @title1 = 'Test types'
    @url1 = '/test_types/'
    @build_types = BuildType.find_all
    @test_type = TestType.find(params[:id])
    @title2 = "Training #{@test_type.name}"
    @url2 = "/test_types/train/#{params[:id]}"
    @test_type = TestType.find(params[:id])
    test_dir = "./repository/tests/#{@test_type.name}-#{@test_type.build_type.title}"
    `rm #{test_dir}/pass/train.tmp && touch #{test_dir}/pass/train.tmp`
    # make sure the train files are blank.
    `rm #{test_dir}/pass/train.in && touch #{test_dir}/pass/train.in`
    `rm #{test_dir}/pass/train.out && touch #{test_dir}/pass/train.out`
    `rm #{test_dir}/pass/train.out2 && touch #{test_dir}/pass/train.out2`
    `rm #{test_dir}/pass/train.err && touch #{test_dir}/pass/train.err`
    
    image = "../images/floppy.img"
    serial_dev2 = '""'
    serial_dev2 = @test_type.serial_dev2 unless @test_type.serial_dev2.nil?
    
    # Fork a runner process.
    fork do
      `cd ./repository/ && ./train_test.rb #{@test_type.timeout} tests/#{@test_type.name}-#{@test_type.build_type.title}/pass #{image} #{@test_type.virtual_machine} #{@test_type.serial_dev1} #{serial_dev2}`
    end
    
  end
  
  def train_input
    @test_type = TestType.find(params[:id])
    test_dir = "./repository/tests/#{@test_type.name}-#{@test_type.build_type.title}"
    if defined? params[:output] and not params[:output].nil? and params[:output].length > 0
      ps = `ps aux`
      fd = File.open "#{test_dir}/pass/train.out"
      @input = fd.read
      fd.close
      if not ps =~ /\.\/train_test\.rb/ then
        fd = File.open "#{test_dir}/pass/train.err"
        err = fd.read
        fd.close
        @input += "</pre><div class=\"error\">Virtual machine has stopped: #{err}</div><pre>"
      end
      return
    end
    fd = File.open "#{test_dir}/pass/train.tmp", "a"
    fd.write params[:input]+"\n"
    fd.close
    fd = File.open "#{test_dir}/pass/train.in", "a"
    fd.write params[:input]+"\n"
    fd.close
    fd = File.open "#{test_dir}/pass/train.in", "r"
    @input = fd.read
    fd.close
  end

  def finish_train
    @test_type = TestType.find(params[:id])
    test_dir = "./repository/tests/#{@test_type.name}-#{@test_type.build_type.title}"
    `rm #{test_dir}/pass/train.tmp`
    
    flash[:notice] = "Test created successfully."
    redirect_to :action => 'list'
  end

  def destroy
    @test_type = TestType.find(params[:id])
    
    `rm -Rf ./repository/tests/#{@test_type.name}-#{@test_type.build_type.title}`
    
    @test_type.destroy
    
    redirect_to :action => 'list'
  end
  
  def delete
  @test_type = TestType.find(params[:id])
    
    `rm -Rf ./repository/tests/#{@test_type.name}-#{@test_type.build_type.title}`
    
    @test_type.destroy
    
    redirect_to :action => 'list'
  end
end
